<?php

namespace App\Http\Controllers\Admins;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Jenson\BaseAdmin\Controllers\JensonBaseAdminController;
use Jenson\BaseAdmin\Libraries\Helper;
use Jenson\BaseAdmin\Models\Admin;

class LoginController extends Controller
{

    /**
     * @param Request $request
     * @return array|Application|Factory|View|RedirectResponse
     * @throws \Exception
     *
     * login
     */
    public function login(Request $request){
        if (session('is_login')) {
            return redirect()->route('admin.default');
        }
        if($request->isMethod('post')){
            # 用户名-密码 登录
            $name = $request->get('username');
            if (!$name) {
                $msg = '请输入登录用户名';
                return AdminRetMsgForPage($msg);
//                return AdminRetError($msg);
            }
            $password = $request->get('password');
            if (!$password) {
                $msg = '请输入登录密码';
                return AdminRetMsgForPage($msg);
//                return AdminRetError($msg);
            }
            $admin = Admin::query()->where('username',$name)->first();
            if (!$admin){
                $msg = '用户名不存在';
                return AdminRetMsgForPage($msg);
//                return AdminRetError($msg);
            }else{
                if ($admin->status != 1){
                    $msg = '该用户已被锁定';
                    return AdminRetError($msg);
                }
                if (password_verify($password,$admin->password)){
                    # 获取用户登录时间,IP储存
                    $loginTime = Carbon::now();
                    $loginIP = $_SERVER['REMOTE_ADDR'];
                    # 将用户登录时间,IP储存
                    Admin::query()->where('id',$admin->id)->update([
                        'last_login_time' => $loginTime,
                        'last_login_ip' => $loginIP,
                    ]);
                    # 将用户信息闪存入session
                    session(['uid' => $admin->id]);
                    session(['username' => $admin->username]);
                    session(['is_login' => true]);
                    # 设置权限信息
                    $jenson = new JensonBaseAdminController($request);
                    $jenson->setUserRolesSession($request,$admin->roles);
                    # 记录登录日志
                    if(config('mbcore_baseadmin.baseadmin_admin_log_add')) {
                        $params = [
                            'admin_id' => $admin['id'],
                            'operation' => '登录',
                            'ip' => $loginIP,
                        ];
                        Helper::addAdminLog($params);
                    }
                    return redirect()->route('admin.default');
//                    return AdminRet([
//                        'url'=>route('admin.default'),
//                    ]);
                }else{
                    $msg = '登录密码错误';
                    return AdminRetMsgForPage($msg);
//                    return AdminRetError($msg);
                }
            }
        }
        return view('v1.admin.login');
    }

    /**
     * @return \Illuminate\Http\RedirectResponse
     * 退出登录
     */
    public function logout(Request $request)
    {
        if ($request->session()->has('is_login')) {
            $is_forgotten = session()->flush(); //删除所有session数据
            if ($is_forgotten === null)
                return redirect()->route('admin.login');
        }
        return redirect()->back()->withErrors('系统异常，退出失败,请重试');
    }
}
